package ecom.appcode.dao.daointerface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ecom.appcode.common.daobean.CategoryDaoBean;
import ecom.appcode.common.daobean.ContactDaoBean;
import ecom.appcode.common.utility.DBUltils;

public class ContactDaoImpl implements ContactDao {
    /** Send Contact */
    public boolean sendContact(String title, String email, String content) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = DBUltils.makeConnection();
            String sql = "INSERT INTO tblContact VALUES(?,?,?,?)";
            /** get Date online */
            Date todayD = new Date(System.currentTimeMillis());
            SimpleDateFormat dayFormat = new SimpleDateFormat("MM/dd/yyyy");
            String todayS = dayFormat.format(todayD.getTime());

            stm = con.prepareStatement(sql);
            stm.setString(1, title);
            stm.setString(2, content);
            stm.setString(3, todayS);
            stm.setString(4, email);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    
    /** View All Contact */
    public List<ContactDaoBean> viewAllContact() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = DBUltils.makeConnection();
            String sql = "SELECT * FROM tblContact";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            List<ContactDaoBean> list = new ArrayList<ContactDaoBean>();
            while (rs.next()) {
                /** Category ID */
                int contactID = rs.getInt("contactID");
                /** Category Name */
                String title = rs.getString("title");
                /** Description */
                String content = rs.getString("content");
                
                String date = rs.getString("date");
                
                String email = rs.getString("email");

                ContactDaoBean contact = new ContactDaoBean(contactID, title, content, date, email);

                list.add(contact);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
    
    /** Delete Contact */
    public boolean deleteContact(int contactID) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "DELETE FROM tblContact WHERE contactID = ?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, contactID);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
